package com.aifund.app.service.impl;

import com.aifund.app.service.RobotService;
import com.aifund.common.dao.AiRobotDao;
import com.aifund.common.entity.AiRobot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
@Service
public class RobotServiceImpl implements RobotService {

    @Autowired
    AiRobotDao aiRobotDao;
    @Override
    public List<AiRobot> getAll(Long exchangeId) {

        Specification specification = new Specification<AiRobot>() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(exchangeId != null){
                    Predicate p1 = criteriaBuilder.equal(root.get("exchangeId"), exchangeId);
                    predicates.add(p1);
                }
                Predicate[] pre = new Predicate[predicates.size()];
                pre = predicates.toArray(pre);
                return criteriaQuery.where(pre).getRestriction();
            }
        };

        List<AiRobot> aiRobots = aiRobotDao.findAll(specification);
        return aiRobots;
    }
}
